package com.loong.system.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.system.common.backend.param.SystemFeedbackQueryParam;
import com.loong.system.common.forehead.param.DeleteFeedbackForeheadParam;
import com.loong.system.common.forehead.param.SystemFeedbackForeheadQueryParam;
import com.loong.system.model.SystemFeedback;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author LiuQun
 * @date 2020-09-28 9:12
 */
@Repository
public class SystemFeedbackBusiness extends AbstractBusiness<SystemFeedback> {

    /**
     *根据参数查询用户反馈信息
     * @param param 查询参数
     * @return 用户反馈集合
     */
    public List<SystemFeedback> findFeedbackByParam(SystemFeedbackForeheadQueryParam param) {
        Condition condition = new Condition(SystemFeedback.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isNotEmpty(param.getUserId())){
            //用户id
            criteria.andEqualTo("userId",param.getUserId());
        }
        if (ObjectUtils.isNotEmpty(param.getUserType())){
            //用户类型
            criteria.andEqualTo("userType",param.getUserType());
        }
        if (ObjectUtils.isNotEmpty(param.getTitle())){
            //标题
            criteria.andLike("title","%"+param.getTitle()+"%");
        }
        if (ObjectUtils.isNotEmpty(param.getFeedbackTypeId())){
            //反馈类型id
            criteria.andEqualTo("feedbackTypeId",param.getFeedbackTypeId());
        }
        if (ObjectUtils.isNotEmpty(param.getReplied())){
            //是否回复
            criteria.andEqualTo("replied",param.getReplied());
        }
        if (ObjectUtils.isNotEmpty(param.getStartTime())){
            //起始时间
            criteria.andGreaterThanOrEqualTo("createTime",param.getStartTime());
        }
        if (ObjectUtils.isNotEmpty(param.getEndTime())){
            //结束时间
            criteria.andLessThanOrEqualTo("createTime",param.getEndTime());
        }
        return findByCondition(condition);
    }

    /**
     * 根据用户id和反馈id删除用户反馈
     * @param userId 用户id
     * @param feedbackId 反馈id
     */
    public void deleteFeedbackByIdParam(Long userId,Long feedbackId){
        Condition condition = new Condition(SystemFeedback.class);
        Example.Criteria criteria = condition.createCriteria();
        //用户id
        criteria.andEqualTo("userId",userId);
        //反馈id
        criteria.andEqualTo("id",feedbackId);
        deleteByCondition(condition);
    }

}
